# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Yuta Nakai <nak5124@live.jp>

_realname=x265
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1
pkgrel=2
pkgdesc='Open Source H265/HEVC video encoder (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc"
   $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-nasm")
)
url='https://bitbucket.org/multicoreware/x265_git'
msys2_changelog_url='https://x265.readthedocs.io/en/master/releasenotes.html'
msys2_references=(
  "cpe: cpe:/a:multicorewareinc:x265"
  "cpe: cpe:/a:multicorewareinc:x265_high_efficiency_video_coding"
)
source=("${_realname}-${pkgver}.tar.gz"::"https://bitbucket.org/multicoreware/x265_git/downloads/x265_${pkgver}.tar.gz")
sha256sums=('a31699c6a89806b74b0151e5e6a7df65de4b49050482fe5ebf8a4379d7af8f29')


prepare() {
  cd "${srcdir}"/x265_${pkgver}

  for d in 8 10 12; do
    if [[ -d "${srcdir}"/build-${MSYSTEM}-${d} ]]; then
      rm -rf "${srcdir}"/build-${MSYSTEM}-${d}
    fi
    mkdir -p "${srcdir}"/build-${MSYSTEM}-${d}
  done
}


build() {
  if [ "${CARCH}" = 'x86_64' ]; then
    _ENABLE_ASM=ON
  else
    _ENABLE_ASM=OFF
  fi

  # 12-bit
  cd "${srcdir}/build-${MSYSTEM}-12"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DHIGH_BIT_DEPTH=ON \
    -DMAIN12=ON \
    -DEXPORT_C_API=OFF \
    -DENABLE_CLI=OFF \
    -DENABLE_SHARED=OFF \
    -DENABLE_ASSEMBLY=${_ENABLE_ASM} \
  ../x265_${pkgver}/source
  ${MINGW_PREFIX}/bin/cmake.exe --build .

  # 10-bit
  cd "${srcdir}/build-${MSYSTEM}-10"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DHIGH_BIT_DEPTH=ON \
    -DEXPORT_C_API=OFF \
    -DENABLE_CLI=OFF \
    -DENABLE_SHARED=OFF \
    -DENABLE_ASSEMBLY=${_ENABLE_ASM} \
  ../x265_${pkgver}/source
  ${MINGW_PREFIX}/bin/cmake.exe --build .

  # 8-bit
  cd "${srcdir}/build-${MSYSTEM}-8"
  ln -s ../build-${MSYSTEM}-10/libx265.a libx265_main10.a
  ln -s ../build-${MSYSTEM}-12/libx265.a libx265_main12.a
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_SHARED=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DENABLE_ASSEMBLY=${_ENABLE_ASM} \
    -DENABLE_HDR10_PLUS=ON \
    -DEXTRA_LIB='x265_main10.a;x265_main12.a' \
    -DEXTRA_LINK_FLAGS='-L .' \
    -DLINKED_10BIT=ON \
    -DLINKED_12BIT=ON \
  ../x265_${pkgver}/source
  ${MINGW_PREFIX}/bin/cmake.exe --build .

  # merge
  mv libx265.a libx265_main.a
  ar -M <<EOF
CREATE libx265.a
ADDLIB libx265_main.a
ADDLIB libx265_main10.a
ADDLIB libx265_main12.a
SAVE
END
EOF
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-8"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Properly install missing libhdr10plus.dll
  install -Dm644 libhdr10plus.dll "${pkgdir}${MINGW_PREFIX}/bin/libhdr10plus.dll"
}
